package cz.myslivec.slovicka.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.JoinColumn;

@Entity
@Table(name="LESSON")
public class Lesson implements Serializable {
	private static final long serialVersionUID = 4450041921008412067L;

	@Id
	@Column(name = "ID_LES")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;

	@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinTable(name="LESS_VOC", 
			joinColumns = {@JoinColumn(name="ID_LES", referencedColumnName = "ID_LES")},
			inverseJoinColumns = {@JoinColumn(name="ID_VOC", referencedColumnName = "ID_VOC")})
	private List<Vocable> vocables;
	
	@Column(name="NAME", nullable=false)
	private String name;
	
	private String note;

	@ManyToOne
	@JoinColumn(name="VOC_TYP_CODE", nullable=false)
	private VocableType vocType;

	public Lesson() {
		super();
	}

	public void addVocable(Vocable vocable) {
		if(this.vocables == null) {
			this.vocables = new ArrayList<Vocable>();
		}
		this.vocables.add(vocable);
	}
	
	public void addVocables(List<Vocable> newVocables) {
		if(this.vocables == null) {
			this.vocables = new ArrayList<Vocable>();
		}
		this.vocables.addAll(newVocables);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<Vocable> getVocables() {
		return vocables;
	}

	public void setVocables(List<Vocable> vocables) {
		this.vocables = vocables;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public VocableType getVocType() {
		return vocType;
	}

	public void setVocType(VocableType vocType) {
		this.vocType = vocType;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Lesson other = (Lesson) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Lesson [id=" + id + ", name=" + name
				+ ", note=" + note + ", vocType=" + vocType + "]";
	}
	
}
